package cn.lingyangwl.agile.food.model.entity;

import cn.lingyangwl.agile.model.BasePO;
import cn.lingyangwl.agile.model.constants.ValidationCons;
import cn.lingyangwl.agile.tenant.core.DataIsolation;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 商品属性
 *
 * @author shenguangyang
 */
@Getter
@Setter
@TableName(value = "food_goods_attribute", autoResultMap = true)
@DataIsolation(filterTenant = true, tableName = "food_goods_attribute")
public class GoodsAttribute extends BasePO {
    @TableId
	private Long id;

	/**
	 * 属性名
	 */
	@ApiModelProperty(value = "属性值")
	@NotNull(message = ValidationCons.NOT_EMPTY)
	@Size(max = 20, message = "名称最大长度为20")
	private String name;

	/**
	 * 属性值
	 */
	@ApiModelProperty(value = "属性值")
	@TableField(value = "`values`", typeHandler = ValuesTypeHandler.class)
	@NotNull(message = ValidationCons.NOT_EMPTY)
	private List<Values> values;

	/**
	 * 是否多选
	 */
	@ApiModelProperty(value = "是否多选")
	@NotNull(message = ValidationCons.NOT_EMPTY)
	private Boolean multipleChoice;

	/**
	 * 是否必填
	 */
	@ApiModelProperty(value = "是否必填")
	@NotNull(message = ValidationCons.NOT_EMPTY)
	private Boolean required;
	
	/**
	 * 排序
	 */
	@ApiModelProperty(value = "排序")
	@NotNull(message = ValidationCons.NOT_EMPTY)
	private Integer sortNo;

	/**
	 * 创建者用户名
	 */
	@TableField(fill = FieldFill.INSERT)
	private String createdBy;

	/**
	 * 创建时间
	 */
	@TableField(fill = FieldFill.INSERT)
	@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
	@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
	private Date createdAt;

	/**
	 * 更新者用户名
	 */
	@TableField(fill = FieldFill.INSERT_UPDATE)
	private String updatedBy;

	/**
	 * 更新时间
	 */
	@TableField(fill = FieldFill.INSERT_UPDATE)
	@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
	@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
	private Date updatedAt;

    public static LambdaQueryWrapper<GoodsAttribute> lqw() {
		return Wrappers.lambdaQuery();
	}

	public static class ValuesTypeHandler extends AbstractJsonTypeHandler<List<Values>> {

		@Override
		protected List<Values> parse(String json) {
			return JSON.parseArray(json, Values.class);
		}

		@Override
		protected String toJson(List<Values> obj) {
			return JSON.toJSONString(obj);
		}
	}

	@Data
	public static class Values {
		/**
		 * 加价
		 */
		private BigDecimal additionalPrice = BigDecimal.ZERO;

		/**
		 * 名称
		 */
		private String name;

		/**
		 * 排序
		 */
		private Integer sortNo = 1;
	}


}
